import axios from "axios";
import { Message } from "element-ui";
// 消息显示的时间
const messageTime =3000
// 实例
const service = axios.create({
    timeout:30000,
    // 测试
    baseURL:'/mock',
})
// 请求拦截
service.interceptors.request.use(
    config=>{
        return config
    },
    error =>{
        Message({
            message:'不能访问服务器',
            type:"error",
            duration:messageTime || 3000,
        })
        return Promise.error(error)
    }
)
// 响应拦截
service.interceptors.response.use(
    response=>{
        if(!response.data || response.data.code !== 200){
            Message({
                message:response.data?.message || '服务器错误',
                type:'error',
                duration:messageTime || 3000,
            })
            return Promise.reject(response.data)
            }
            return Promise.resolve(response.data)
        },
    error =>{
        Message({
            message:error.message,
            type:"error",
            duration: messageTime ||3000,
        })
        return Promise.reject(error)
    }
)

export default service
